#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <ctype.h>



int main(int argc, char *argv[])
{
    int lfd,cfd;
    int ret,i;
    char buf[200];
    
	struct sockaddr_in srv_addr;		// 创建服务器地址结构,初始化
	
	srv_addr.sin_family = AF_INET;
	srv_addr.sin_port = htons(8888);
	srv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    

    //1.创建套接字
    lfd = socket(AF_INET , SOCK_STREAM,0);
    if(lfd == -1)
    {
        perror("socket err");
        return -1;
    }
    //2.绑定服务器IP和端口号
    if(bind(lfd,(struct sockaddr*)&srv_addr, sizeof(srv_addr))==-1)
    {
    	perror("bind");
    	return -1;
    }
    //3.设置监听上限
    if(listen(lfd,5)==-1)
    {
    	perror("listen");
    	return -1;
    }
    printf("服务器等待客户端链接...\n");
    
    //4.阻塞等待客户端连接请求
	cfd = accept(lfd,NULL, NULL);
		 if(cfd == -1)
    {
    	perror("accept");
    	return -1;
    }
	printf("连接成功\n");
	while (1) {
		// 5.read() 读取客户端发送数据
	read(cfd, buf, sizeof(buf));
	printf("接受到的信息为：%s\n", buf);
	
}
}
